package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.Smslog;
import com.qianniu.napi.admin.service.ISmslogService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2019-09-09
 */
@Controller
public class SmslogController {

    private static final Logger logger = LoggerFactory.getLogger(SmslogController.class);

    @Autowired
    private ISmslogService smslogService;

    @RequestMapping("admin/smslog/index")
    public String index(Model model) {
        return "admin/smslog";
    }

    @RequestMapping("/admin/smslog/list")
    @ResponseBody
    public Page<Smslog> list(@RequestParam(value = "offset", required = false) Integer offset,
                             @RequestParam(value = "status", required = false) Integer status,
                             @RequestParam(value = "search", required = false) String search,
                             @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<Smslog> wrapper = new EntityWrapper<Smslog>();
        wrapper.eq("mpid", ObjectUtil.getMpid());

        wrapper.orderBy("id", false);
        Page<Smslog> page = smslogService.selectPage(new Page<Smslog>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/smslog/add")
    public String add(@Valid Smslog bean, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.setState(1);
        bean.insert();
        return "admin/smslog";
    }

    @RequestMapping("/admin/smslog/getSmslogById")
    @ResponseBody
    public Smslog getSmslogById(long id) {
        Smslog smslog = smslogService.selectById(id);
        return smslog;
    }

    @RequestMapping("/admin/smslog/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = smslogService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/smslog/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Smslog bean = new Smslog();
        bean.setId(id);
        bean.setState(state);
        smslogService.updateById(bean);
        return "success";
    }

}
